package com.lianrui.app.comments.controller;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lianrui.app.comments.domain.Comments;
import com.lianrui.app.comments.service.ICommentsService;
import com.lianrui.app.comments.vos.CommentsPage;
import com.lianrui.app.goods.controller.GoodsController;
import com.lianrui.commons.ResultView;
import com.lianrui.commons.exception.BusinessException;
import com.lianrui.core.AbstractController;
import com.lianrui.core.IService;

@Controller
@RequestMapping("/comments")
public class CommentsController extends AbstractController<CommentsPage, Comments, Integer>{
	
	private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);

	@Autowired
	private ICommentsService commentsService;

	@Override
	public IService<CommentsPage, Comments, Integer> getService() {
		return commentsService;
	}

	@Override
	public Logger getLogger() {
		return LOGGER;
	}
	
	@RequestMapping("/forOrder")
	@ResponseBody
	public ResultView<Integer> addOrderComment(Comments comments) {
		ResultView<Integer> view = new ResultView<Integer>();
		comments.setComment(comments.getComment()==null?"":comments.getComment().replaceAll("<", "《").replaceAll(">", "》"));
		comments.setTime((new Date()).getTime());
		comments.setDisplay("true");
		comments.setObject_type("discuss");
		comments.setCommenttype("goods");
		comments.setMem_read_status("false");
		comments.setDisplay("true");
		comments.setAdm_read_status("false");
		comments.setDisabled("false");
		if(comments.getFor_comment_id()==null){
			comments.setGrade(0);
			comments.setGrade1(0);
			comments.setGrade2(0);
		}
		try {
			getService().add(comments);
			view.setResult(comments.getId());
		} catch (BusinessException e) {
			view.setCode(e.getCode());
			view.setMessage(e.getMessage());
		}
		return view;
	}
}
